<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/c/font_2592779_u9zya6masq.css">
    <script src="./data/searchlib.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            color: #333;
            padding-top: 50px;
        }

        ul {
            list-style: none;
        }

        .out_box {
            width: 60%;
            margin: auto;
        }

        .search_box {
            position: relative;
            padding: 10px;
        }

        .search_box .iconfont {
            position: absolute;
            right: 4px;
            top: 17px;
            font-size: 30px;
            cursor: pointer;
        }

        .search_box input {
            outline: none;
            width: 100%;
            height: 40px;
            border: 1px solid #ccc;
            border-radius: 6px;
            padding: 0 10px 0 10px;
        }

        .his_list_box {
            display: flex;
            padding: 10px;
            color: #ccc;
        }

        .his_list_box p {
            width: 75px;
        }

        .his_list {
            display: flex;
            flex-wrap: wrap;
            flex: 1;
            min-height: 30px;
        }

        .his_list li {
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 4px 8px;
            margin-right: 10px;
        }

        .his_list li span {
            color: #333;
        }

        .his_list li .iconfont {
            cursor: pointer;
            margin-left: 10px;
        }

        .result {
            padding: 10px;
        }

        .keyworld {
            color: red;
        }

        .res_list {
            padding: 10px;
        }

        .res_list li {
            padding: 10px 0 10px 0;
            border-bottom: 1px dashed #ccc;
        }
    </style>
</head>

<body>
    <div class="out_box">
        <div class="search_box">
            <i class="iconfont icon-search"></i>
            <input type="text" id="search_value" placeholder="请输入关键字">
        </div>
        <div class="his_list_box">
            <p>搜索历史</p>
            <ul class="his_list">
                <!-- <li>
                    <span>一只糖果</span>
                    <i class="iconfont icon-shanchu"></i>
                </li> -->
            </ul>
        </div>
        <div class="result">
            <p>搜索结果：</p>
            <ul class="res_list">
                <!-- <li>这是一个<span class="keyworld">无效</span>的消息</li> -->
            </ul>
        </div>
    </div>
    <script>
        var search_value = document.getElementById('search_value');
        var search_btn = document.querySelector('.icon-search');
        var res_list = document.querySelector('.res_list');
        var his_list = document.querySelector('.his_list');

        search_value.onkeyup = function (e) {
            if (e.which === 13) {
                search();
            }
        }
        search_btn.onclick = search;
        var search_his = [];
        function search() {
            var keyworld = search_value.value;
            if (keyworld.trim() === '') {
                res_list.empty();
                return;
            }
            if (!search_his.includes(keyworld)) {
                search_his.push(keyworld);
            }
            var temp = '';
            search_his.forEach((item, index) => {
                temp += `<li>
                    <span>${item}</span>
                    <i data-index="${index}" class="iconfont icon-shanchu"></i>
                </li>`
            })
            his_list.innerHTML = temp;

            temp = '';
            var list = searchLib.filter(item => item.includes(keyworld))
            list.forEach(item => {
                let txt = item.replaceAll(keyworld, '<span class="keyworld">' + keyworld + '</span>')
                temp += `<li>${txt}</li>`;
            })
            if (list.length === 0) {
                temp += `<li>当前没有找到对应的结果 ~</li>`
            }
            res_list.innerHTML = temp;

            search_value.value = ''
        }

        his_list.onclick = function (e) {
            if (e.target.classList.contains('iconfont')) {
                let idx = parseInt(e.target.dataset.index);
                search_his.splice(idx, 1)
                e.target.parentNode.remove();
            }
        }
    </script>
</body>

</html>